﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SHVS.Media;
namespace GMPP.Texture
{
    /// <summary>
    /// A material definition.
    /// </summary>
    /// <remarks>
    /// The file format is documented in http://en.wikipedia.org/wiki/Material_Template_Library.
    /// </remarks>
    public class MaterialDefinition
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="MaterialDefinition"/> class.
        /// </summary>
        public MaterialDefinition()
        {
            this.Dissolved = 1.0;
        }

        /// <summary>
        /// Gets or sets the alpha map.
        /// </summary>
        /// <value>The alpha map.</value>
        public string AlphaMap { get; set; }

        /// <summary>
        /// Gets or sets the ambient color.
        /// </summary>
        /// <value>The ambient.</value>
        public Color Ambient { get; set; }

        /// <summary>
        /// Gets or sets the ambient map.
        /// </summary>
        /// <value>The ambient map.</value>
        public string AmbientMap { get; set; }

        /// <summary>
        /// Gets or sets the bump map.
        /// </summary>
        /// <value>The bump map.</value>
        public string BumpMap { get; set; }

        /// <summary>
        /// Gets or sets the diffuse color.
        /// </summary>
        /// <value>The diffuse.</value>
        public Color Diffuse { get; set; }

        /// <summary>
        /// Gets or sets the diffuse map.
        /// </summary>
        /// <value>The diffuse map.</value>
        public string DiffuseMap { get; set; }

        /// <summary>
        /// Gets or sets the opacity value.
        /// </summary>
        /// <value>The opacity.</value>
        /// <remarks>
        /// 0.0 is transparent, 1.0 is opaque.
        /// </remarks>
        public double Dissolved { get; set; }

        /// <summary>
        /// Gets or sets the illumination.
        /// </summary>
        /// <value>The illumination.</value>
        public int Illumination { get; set; }

        /// <summary>
        /// Gets or sets the specular color.
        /// </summary>
        /// <value>The specular color.</value>
        public Color Specular { get; set; }

        /// <summary>
        /// Gets or sets the specular coefficient.
        /// </summary>
        /// <value>The specular coefficient.</value>
        public double SpecularCoefficient { get; set; }

        /// <summary>
        /// Gets or sets the specular map.
        /// </summary>
        /// <value>The specular map.</value>
        public string SpecularMap { get; set; }

        /// <summary>
        /// Gets or sets the material.
        /// </summary>
        /// <value>The material.</value>
        public Texture Material { get; set; }

        /// <summary>
        /// Gets the material from the specified path.
        /// </summary>
        /// <param name="texturePath">
        /// The texture path.
        /// </param>
        /// <returns>
        /// The material.
        /// </returns>
        /*   public Texture GetMaterial(string texturePath)
           {
               if (this.Material == null)
               {
                   this.Material = this.CreateMaterial(texturePath);
                   this.Material.Freeze();
               }

               return this.Material;
           }*/

        /*  /// <summary>
          /// Creates the material.
          /// </summary>
          /// <param name="texturePath">The texture path.</param>
          /// <returns>A WPF material.</returns>
          private Texture CreateMaterial(string texturePath)
          {
              var mg = new MaterialGroup();

              // add the diffuse component
              if (this.DiffuseMap == null)
              {
                  var diffuseBrush = new SolidColorBrush(this.Diffuse) { Opacity = this.Dissolved };
                  mg.Children.Add(new DiffuseMaterial(diffuseBrush));
              }
              else
              {
                  var path = Path.Combine(texturePath, this.DiffuseMap);
                  if (File.Exists(path))
                  {
                      mg.Children.Add(new DiffuseMaterial(this.CreateTextureBrush(path)));
                  }
              }

              // add the ambient components
              if (this.AmbientMap == null)
              {
                  // ambient material is not supported by WPF?
              }
              else
              {
                  var path = Path.Combine(texturePath, this.AmbientMap);
                  if (File.Exists(path))
                  {
                      mg.Children.Add(new EmissiveMaterial(this.CreateTextureBrush(path)));
                  }
              }

              // add the specular component
              if (this.Specular.R > 0 || this.Specular.G > 0 || this.Specular.B > 0)
              {
                  mg.Children.Add(new SpecularMaterial(new SolidColorBrush(this.Specular), this.SpecularCoefficient));
              }

              return mg.Children.Count != 1 ? mg : mg.Children[0];
          }*/

        /// <summary>
        /// Creates a texture brush.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns>The brush.</returns>
        /*      private ImageBrush CreateTextureBrush(string path)
              {
                  var img = new BitmapImage(new Uri(path, UriKind.Relative));
                  var textureBrush = new ImageBrush(img) { Opacity = this.Dissolved, ViewportUnits = BrushMappingMode.Absolute, TileMode = TileMode.Tile };
                  return textureBrush;
              }*/

    }
}
